package com.bifrii.bo;

import java.util.List;

import com.bifrii.dao.ICategoryCommunityDAO;
import com.bifrii.dao.ICommunityDAO;
import com.bifrii.dao.IRoleDAO;
import com.bifrii.dao.JDBCCategoryCommunityDAO;
import com.bifrii.dao.JDBCCommunityDAO;
import com.bifrii.dao.JDBCRoleDAO;
import com.bifrii.dao.UIDGenerator;
import com.bifrii.transferObjects.CategoryCommunity;
import com.bifrii.transferObjects.Event;
import com.bifrii.transferObjects.Role;
import com.bifrii.transferObjects.User;
import com.bifrii.transferObjects.Community;


public class CommunityManager {
	private static ICommunityDAO community;
	private static ICategoryCommunityDAO category;
	private static CommunityManager cm;
	private static UIDGenerator oid;
	private static Integer top = 3;
	private static IRoleDAO rol;
	public static synchronized CommunityManager getInstance(){
		if (cm==null)
			cm = new CommunityManager();
		return cm;
	}
	
	private CommunityManager(){
		community = new JDBCCommunityDAO();
		category = new JDBCCategoryCommunityDAO();
		rol = new JDBCRoleDAO();
	}
	public boolean testCommunity(String name){ // este metodo nos dira si una comunidad existe o no
		boolean ret = true;
		List<Community> communities = community.selectAllCommunities();
		for(Community c:communities){
			
			if(c.getName().equals(name)){
				
				ret=false;
			}
		}
		return ret;
	}
	public void newCommunity(Integer idCommunity ,Integer idCreator , String name,int category , String description, String avatar){
		Community c = new Community();
		c.setName(name);
		c.setIdCreator(idCreator);
		c.setOid(oid.getInstance().getKey());// FIX THIS
		c.setIdCategory(category);
		c.setDescription(description);
		c.setAvatar(avatar);
		community.insert(idCommunity, c); // FIX THIS
		
	}
	public List<User> getCommunityUsers(Integer idCommunity){
		
		return community.selectAllUsers(idCommunity);
	} 
	public List<Event> getCommunityEvents(Integer idCommunity){
		return community.selectEvents(idCommunity);
	}
	public void deleteCommunity(Integer idCommunity){
		community.delete(idCommunity);
	}
	public List<Community> getAllCommunities(){
		return community.selectAllCommunities();
	}
	public Community getCommunity(Integer idCommunity){
		
		return community.select(idCommunity);
		
	}
	public List<Community> getTopCommunities(){
		return community.selectTopCommunities(top);
	}
	public boolean addUser(Integer idCommunity,Integer idUser,Integer idRole){
		boolean ret=true;
		community.insertUser(oid.getInstance().getKey(),idCommunity, idUser, idRole);
		return ret;
		}
	public List<CategoryCommunity> getAllCategories(){
		return category.selectAll();
	}
	
	public List<Community> getAllCommunitiesForUser(Integer idUser){
		return community.selectAllCommunitiesForUser(idUser);
	}
	
	public List<Role> getAllRoles()
			{return rol.selectAllRoles();}
}
